﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DalatCooking.Core;
using DalatCooking.Core.Helper;
public partial class admin_Course_Add_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
        LoadParentListBox(lbCategories, 0);
        lbCategories.SelectedIndex = 0;
        BinAuthors();
        BinCourseImage();
        if (CourseID != -1)
        {
            BinDataInfo();
        }
        base.OnInit(e);
    }
    public void BinAuthors()
    {
        ddlAuthor.DataSource = PersonManager.FindAll();
        ddlAuthor.DataTextField = "UserName";
        ddlAuthor.DataValueField = "PersonId";
        ddlAuthor.DataBind();
    }
    void BinDataInfo()
    {
        Course c = CourseManager.FindById(CourseID);
        if (c != null)
        {
            txtName.Text = c.Name;
            txtDescription.Text = c.Description;
            txtBody.Text = c.Body;
            txtDate.Text = c.DateCreated.ToString();
            txtSlug.Text = c.Name;
            imgthumb.Src = c.Picture;
            txtPicture.Text = c.Picture;
            lbCategories.SelectedValue = c.CategoryId.ToString();
            cbIsActive.Checked = (bool)c.IsActive;
           // cbEnableComments.Checked = (bool)c.AllowComment;
           // cbIsFrontPage.Checked = (bool)c.IsFrontPage;
            txtSortIndex.Text = c.SortIndex.ToString();
           
        }
    }
    void BinCourseImage()
    {
        if (CourseID != -1)
        {
            rptImageSlide.DataSource = CourseImageManager.FindAll().FindAll(c => c.IsPublished = true && c.CourseID == CourseID);
            rptImageSlide.DataBind();
        }
        else
        {
            rptImageSlide.DataSource = CourseImageManager.FindAll().FindAll(c => c.IsPublished = true);
            rptImageSlide.DataBind();
        }

    }
    public int CourseID
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["CourseID"]))
            {
                return ConvertObject.ToInt(Request.QueryString["CourseID"]);
            }
            return -1;
        }
    }
    int level = -1;
    void LoadParentListBox(DropDownList ddl, int parentID)
    {
        level++;
        System.Text.StringBuilder appender = new System.Text.StringBuilder();
        for (int j = 0; j < level; j++)
        {
            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;");
        }
        if (level > 0)
        {
            appender.Append("|__");
        }
        foreach (var i in CategoryManager.FindAll().Where(c => c.ParentId==parentID&& c.IsActive == true))
        {
            ListItem l = new ListItem();
            l.Value = i.CategoryId.ToString();
            l.Text = HttpUtility.HtmlDecode(appender.ToString()) + i.Name;
            l.Attributes.Add("data-image", "ThumbnailHandler.axd?img=" +i.Picture + "");
            l.Attributes.Add("data-description", HttpUtility.HtmlDecode(appender.ToString()) + i.Name);

            // ddl.Items.Add(new ListItem(HttpUtility.HtmlDecode(appender.ToString()) + list[i].Name, list[i].CategoryId.ToString()));

            ddl.Items.Add(l);
           // ddl.Items.Add(new ListItem(Server.HtmlDecode(appender.ToString()) + i.Name, i.CategoryId.ToString()));
            LoadParentListBox(ddl, i.CategoryId);
            //foreach (var j in Categories.Parent(i.CategoryID))
            //{
            //    ddl.Items.Add(new ListItem("----" + j.CategoryName, j.CategoryID.ToString()));
            //}
        }
        level--;
    }

    void UploadImage(int CourseId)
    {
        string path = "";
        string errMess = "";
        HttpFileCollection hfc1 = Request.Files;

        for (int i = 0; i < hfc1.Count; i++)
        {
            HttpPostedFile hpf = hfc1[i];
            // Upload Images
            if (DalatCooking.Core.Web.HttpHandlers.ImageUploadBase.ImageUpload(hpf, ref errMess, ref path) == true)
            {
                CourseImage s = new CourseImage();
                s.CourseID = CourseID;
                s.ImageUrl = path;
                s.Alt = "";
                s.Title = txtName.Text;
                s.IsPublished = true;
                bool rs = CourseImageManager.Insert(s);
            }
        }
    }

    int Insert()
    {
        Course c = new Course();
        c.CourseId = -1;
        c.CategoryId = ConvertObject.ToInt(lbCategories.SelectedValue);
        c.Name = txtName.Text;
        c.Description = txtDescription.Text;
        c.Body = txtBody.Text;
        c.Picture = txtPicture.Text;
        c.IsActive = cbIsActive.Checked;
        c.CreatedBy = ddlAuthor.Text;
        c.CreatedById = ConvertObject.ToInt(ddlAuthor.SelectedValue);
        c.Price = ConvertObject.ToFloat(txtPrice.Text);
        c.IsFree = cbIsPromotion.Checked;
        int rs = CourseManager.Insert(c);
        return rs;
    }
    int Update()
    {
        Course c = CourseManager.FindById(CourseID);        
        c.CategoryId = ConvertObject.ToInt(lbCategories.SelectedValue);
        c.Name = txtName.Text;
        c.Description = txtDescription.Text;
        c.Body = txtBody.Text;
        c.Picture = txtPicture.Text;
        c.IsActive = cbIsActive.Checked;
        c.CreatedBy = ddlAuthor.Text;
        c.CreatedById = ConvertObject.ToInt(ddlAuthor.SelectedValue);
        c.Price = ConvertObject.ToFloat(txtPrice.Text);
        c.IsFree = cbIsPromotion.Checked;
        c.SortIndex = ConvertObject.ToInt(txtSortIndex.Text);
        try
        {
            int rs = CourseManager.Update(c);
            return rs;
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        return -1;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
       
        if (CourseID != -1)
        {
            if (Update() > 0)
            {

                UploadImage(CourseID);
                Response.Redirect("Default.aspx");
                // Response.Redirect(Session["ProductHistoryLink"].ToString());
            }
        }
        else
        {
            int kq = Insert();
            if (kq > 0)
            {              
                if (!FileUpload1.HasFile)
                {
                    CourseImage s = new CourseImage();
                    s.CourseID = kq;
                    s.ImageUrl = txtPicture.Text;
                    s.Alt = "";
                    s.Title = txtName.Text;
                    s.IsPublished = true;
                    CourseImageManager.Insert(s);
                }
                else
                {
                    UploadImage(kq);
                }
                Response.Redirect("Default.aspx");
                // Response.Write(Save().ToString());
            }
        }    
    }
    protected void btnCategory_Click(object sender, EventArgs e)
    {

    }
}